import asyncio
import os
from ygg_aiomysql.db import new_pool_env

async def main():
    """
    从环境变量创建MySQL连接池并查询数据库
    """
    # 设置环境变量(示例)
    os.environ['MYSQL_HOST'] = 'localhost'
    os.environ['MYSQL_PORT'] = '3306'
    os.environ['MYSQL_USER'] = 'root'
    os.environ['MYSQL_PASSWORD'] = 'zhangdapeng520'
    os.environ['MYSQL_DB'] = 'test'

    # 创建连接池
    pool = await new_pool_env()
    
    async with pool.acquire() as conn:
        async with conn.cursor() as cur:
            # 查询所有数据库
            await cur.execute("SHOW DATABASES")
            databases = await cur.fetchall()
            print("从环境变量创建的连接池查询结果:")
            for db in databases:
                print(db[0])
    
    # 关闭连接池
    pool.close()
    await pool.wait_closed()

if __name__ == '__main__':
    asyncio.run(main())